博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[MST] Build Forms with React to Edit mobx-state-tree Models
阅读量:5798 次
发布时间:2019-06-18

本文共 2606 字,大约阅读时间需要 8 分钟。

We will expand our UI, and give the user the possibility to edit his wishlist. We will use the earlier defined actions. We also will use model clones only to modify data temporarily, and after approving the changes, apply them back to the original model.

In this lesson you will learn:

  • How to call model actions from a component
  • Using clone to create a full, deep clone of any model instance
  • Using applySnapshot to update the state of a model instance given a snapshot.

 

The whole point for building a editing form component is that:

  1. avoid two ways data flow, means that you change the data inside the form, without saving but the data was mutated already. To solve this problem, we will use 'clone' from 'mobx-state-tree'.

  2. When save the data, we can use 'getSnapshot' and 'applySnapshot' from the lib.

import React, {Component} from "react"import {observer} from "mobx-react";import {clone, getSnapshot, applySnapshot} from 'mobx-state-tree';import WishListItemEdit from "./WishListItemEdit"class WishListItemView extends Component {    constructor() {        super();        this.state = {isEditing: false}    }    render() {        const {item} = this.props;        return this.state.isEditing ?            this.renderEditable() :            this.renderItemView(item);    }    renderEditable() {        return (            
  • ); } renderItemView(item) { return (
  • {item.image && }

    {item.name}

    {item.price}
  • ); } onSaveEdit = () => { applySnapshot(this.props.item, getSnapshot(this.state.clone)) this.setState({ isEditing: false, clone: null }) }; onCancelEdit = () => { this.setState({isEditing: false}) }; onToggleEdit = () => { this.setState({ isEditing: true, clone: clone(this.props.item) }) }}export default observer(WishListItemView)

     

    Here we have to use the methods provide from the lib, because the 'this.props.item' is a mobx state model object:

    {$mobx: ObservableObjectAdministration, toString: ƒ, …}

     

    The good part of this approach is that, the model related data can be handled by the mobx-state-tree lib. It can helps to udpate the model efficiently.

    The down side of the approach is that,  you needs to keep at least tow parts of state, one is mobx-state-tree, another one is the component state, for example, in the code, 'isEiditing' & 'clone'. 

     

    转载地址:http://amifx.baihongyu.com/

    你可能感兴趣的文章
    USNews大学排名遭美国计算机研究学会怒怼,指排名荒谬要求撤回
    查看>>
    struts1——静态ActionForm与动态ActionForm
    查看>>
    七大关键数据 移动安全迎来历史转折点
    查看>>
    各大电商纷纷瞄准机器人领域,备战双十一各显神功
    查看>>
    在AngularJS中学习javascript的new function意义及this作用域的生成过程
    查看>>
    盘点物联网网关现有联网技术及应用场景
    查看>>
    Windwos 08R2_DNS全面图文详解
    查看>>
    重拾黑客精神:后IT时代技术流的回归
    查看>>
    网络钓鱼大讲堂 Part3 | 网络钓鱼攻击向量介绍
    查看>>
    阿里云与Intel联合发布加密计算,亚洲首个云上“芯片级”数据保护
    查看>>
    SQL Server 2005故障转移群集
    查看>>
    js 动态添加input代码
    查看>>
    oldboy 27期学习计划
    查看>>
    我的友情链接
    查看>>
    Caused by: javax.el.ELException:
    查看>>
    我的友情链接
    查看>>
    【Nocti推荐】Sublime text!超赞代码编辑器
    查看>>
    《笨方法学Python》ex22(1)
    查看>>
    js实现按钮复制功能
    查看>>
    1、下载安装scala编译器(可以理解为scala的jdk),地址:http://www.scala
    查看>>